package com.crazyhat.crazyhat.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.imageaudit20191230.models.ScanImageResponseBody.ScanImageResponseBodyDataResults;
import com.crazyhat.crazyhat.pojo.Result;
import com.crazyhat.crazyhat.utils.ObsUtil;
import com.crazyhat.crazyhat.utils.ScanImage;

import jakarta.servlet.http.HttpServletResponse;




@RestController
@RequestMapping("/test")
public class FileUploadController {
    
    /**
     * 上传文件的父目录名
     * 例如：img/
     */
    @Value("${my-config.aliyun-OSS-config.imageDirectory}")
    private String imageDirectory;

    /**
     * 首页
     * @return 登录页面
     */
    @RequestMapping("/")
    public String index(HttpServletResponse response){

        //TODO:考虑替换或重定向?

        return "html/login";
    }

    /**
     * 上传文件
     * @param fileList 文件列表
     * @return 文件url列表
     */
    @PostMapping("/uploadFile")
    public Result uploadFile(List<MultipartFile> fileList) {
        
        try{

            List<String> fileUrlList=new ArrayList<>();

            for(MultipartFile file:fileList) {
                String originalFileName=file.getOriginalFilename();

                if (originalFileName==null) {
                    continue;
                }

                /**
                 * 文件名替换为uuid，并添加父目录名
                 */
                String savedfileName = imageDirectory+UUID.randomUUID().toString()
                        + originalFileName.substring(originalFileName.lastIndexOf("."));

                fileUrlList.add(ObsUtil.uploadFile(savedfileName, file.getInputStream()));
            }

            return Result.success(fileUrlList);

        } catch(IOException e) {

            e.printStackTrace();

            return Result.error(e.getMessage());
        }
    }

    /**
     * 违规检测
     * @param fileList 文件列表
     * @return 违规检测结果
     */
    @PostMapping("/violationDetection")
    public Result violationDetection(List<MultipartFile> fileList) {

        List<ScanImageResponseBodyDataResults> resultList=ScanImage.scanImageList(fileList);

        if (resultList!=null) {
            return Result.success(resultList);
        } else {
            return Result.error("图片违规检测失败");
        }
    }

    /**
     * 判断是否为正常图片
     * @param file 文件
     * @param strict 是否严格检测
     * @return 是否为正常图片
     */
    @PostMapping("/isNormalImage")
    public Result<String> isNormalImage(MultipartFile file,@RequestParam(value = "strict",required = false) Boolean strict) {

        if (strict==null) {
            strict=true;
        }

        if (ScanImage.scanAndApprove(file,strict)) {
            return Result.success("图片不违规");
        } else {
            return Result.success("图片违规");
        }
    }

    /**
     * 删除文件
     * @param objectUrl 文件url
     * @return 是否删除成功
     */
    @PostMapping("/deleteFile")
    public Result deleteFile(@RequestParam(value = "objectUrl") String objectUrl) {

        if (ObsUtil.deleteFile(objectUrl)) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }
}
